package bcel.cardcenter.bconnex.recon.dao;

import java.sql.Date;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import bcel.cardcenter.bconnex.recon.entity.BconTxn;
@Repository("bconTxnDao")
public class BconTxnDaoImp implements BconTxnDao {
	private HibernateTemplate hibernateTemplate;
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory){
		this.hibernateTemplate = new HibernateTemplate(sessionFactory);
	}
	@Transactional
	@Override
	public void save(BconTxn bconTxn) throws SQLException, HibernateException {
		hibernateTemplate.save(bconTxn);
	}
	@Transactional
	@Override
	public void saveAll(List<BconTxn> bconTxns) throws SQLException,
			HibernateException {
		hibernateTemplate.saveOrUpdateAll(bconTxns);
	}
	@Override
	public List<BconTxn> getBconTxnReport(Date date) throws SQLException,
			HibernateException {
		String hql = "select count(*) as NUM from BconTxn where";
		return toList(hibernateTemplate.findByNamedParam(hql, "date", date));
	}
	private List<BconTxn> toList(List<?> beans){
		if(beans == null) return null;
		if(beans.isEmpty()) return null;
		int size = beans.size();
		BconTxn[] list = new BconTxn[size];
		list = (BconTxn[])beans.toArray(list);
		return Arrays.asList(list);
	}
}
